package com.lg.customRxJava;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.Objects;



public abstract class Observable< T> implements ObservableSource<T> {
    private static  String TAG = Observable.class.getSimpleName();

    static volatile Function<? super Observable, ? extends Observable> onObservableAssembly;

    public static <T> Observable<T> just( T item) {
        //Objects.requireNonNull(item, "item is null");
        return onAssembly(new ObservableJust<>(item));

        // new ObservableJust<>(item);
        //return null;
    }

    protected abstract void subscribeActual( Observer<? super T> observer);


    public static <T> Observable<T> onAssembly(Observable<T> source) {
        Function<? super Observable, ? extends Observable> f = onObservableAssembly;
        if (f != null) {
            return apply(f, source);
        }
        return source;
    }

    static <T, R> R apply( Function<T, R> f,  T t) {
        try {
            return f.apply(t);
        } catch (Throwable ex) {
            throw wrapOrThrow(ex);

        }
    }

    public final <R> Observable<R> map(Function<? super T, ? extends R> mapper) {
        return onAssembly(new ObservableMap<>(this, mapper));
    }

    public final Observable<T> subscribeOn( Scheduler scheduler) {
        Objects.requireNonNull(scheduler, "scheduler is null");
//        return  onAssembly(new ObservableSubscribeOn<>(this, scheduler));
        return null;
    }

    public static RuntimeException wrapOrThrow(Throwable error) {
        if (error instanceof Error) {
            throw (Error)error;
        }
        if (error instanceof RuntimeException) {
            return (RuntimeException)error;
        }
        return new RuntimeException(error);
    }


    public final Observable<T> observeOn(Scheduler scheduler) {
        return observeOn(scheduler, false, BUFFER_SIZE);
    }

    public final Observable<T> observeOn(Scheduler scheduler, boolean delayError, int bufferSize) {
//        return onAssembly(new ObservableObserveOn<>(this, scheduler, delayError, bufferSize));
        return null;
    }

    static final int BUFFER_SIZE;
    static {
        BUFFER_SIZE = Math.max(1, Integer.getInteger("rx3.buffer-size", 128));
    }

    private static volatile Function<Scheduler, Scheduler> onMainThreadHandler;

    public static Scheduler onMainThreadScheduler(Scheduler scheduler) {
        if (scheduler == null) {
            throw new NullPointerException("scheduler == null");
        }
        Function<Scheduler, Scheduler> f = onMainThreadHandler;
        if (f == null) {
            return scheduler;
        }
        return apply(f, scheduler);
    }





}
